import json
import random
from datetime import datetime

from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from ..forms import OrderModelForm
from .. import models
from ..utils.pagination import Pagination


def order_list(request):
    queryset = models.Order.objects.all().order_by("-id")
    page_object = Pagination(request, queryset)
    form = OrderModelForm()
    context = {
        "queryset": page_object.page_queryset,  # 分完页的数据
        "page_string": page_object.html(), # 页码
        "form": form
    }
    return render(request, "app_02/order_list.html", context)


@csrf_exempt
def order_add(request):
    """ 新建订单（Ajax请求） """
    form = OrderModelForm(data=request.POST)
    if form.is_valid():
        # 订单号: 额外添加一些不是用户输入的值(自己计算出来)
        form.instance.oid = datetime.now().strftime("%Y%m%d%H%M%S") + str(random.randint(1000, 9999))

        # 固定设置管理员ID，去哪里获取？

        form.instance.admin_id = request.session["info"]["id"]
        # form.instance
        form.save()
        # return HttpResponse(json.dumps({"status": True})) # 等同于下面那个
        return JsonResponse({"status": True})
    return JsonResponse({"status": False, "error": form.errors})


def order_delete(request):
    uid = request.GET.get("uid")
    exists = models.Order.objects.filter(id=uid).exists()
    if not exists:
        return JsonResponse({"status": False, "error": "数据不存在"})

    models.Order.objects.filter(id=uid).delete()
    return JsonResponse({"status": True})


def order_detail(request):
    """根据ID获取订单详细"""
    uid = request.GET.get("uid")

    # 对象，当前行的所有数据
    row_dict = models.Order.objects.filter(id=uid).values("title", "price", "status").first()
    if not row_dict:
        return JsonResponse({"status": False, "error": "数据不存在"})

    # 从数据库中获取到一个对象row_object
    result = {
        "status": True,
        "data": row_dict
    }
    return JsonResponse(result)


@csrf_exempt
def order_edit(request):
    """编辑订单"""
    if request.method == "GET":
        uid = request.GET.get("uid")
        
    row_object = models.Order.objects.filter(id=uid).first()
    if not row_object:
        return JsonResponse({"status": False, "tips": "数据不存在， 请重新刷新"})

    form = OrderModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return JsonResponse({"status": True})

    return JsonResponse({"status": False, "error": form.errors})









